home *** CD-ROM | disk | FTP | other *** search
/ Apple WWDC 1996 / WWDC96_1996 (CD).toast / Technology Materials / MacApp Release 10 / MacApp Release 10 - HD Ready / Libraries / Views / Sources / UDialog.More.cp < prev    next >
Encoding:
Text File  |  1996-04-03  |  35.6 KB  |  1,387 lines  |  [TEXT/MPS ]

  1. //----------------------------------------------------------------------------------------
  2. // UDialog.More.cp 
  3. // Copyright © 1988-96 by Apple Computer, Inc. All rights reserved.
  4. //----------------------------------------------------------------------------------------
  5.  
  6. #ifndef __UDIALOG__
  7. #include "UDialog.h"
  8. #endif
  9.  
  10. // MacApp
  11.  
  12. #ifndef __MACAPPTYPES__
  13. #include "MacAppTypes.h"
  14. #endif
  15.  
  16. #ifndef __UADORNERS__
  17. #include "UAdorners.h"
  18. #endif
  19.  
  20. #ifndef __UASSOCIATION__
  21. #include "UAssociation.h"
  22. #endif
  23.  
  24. #ifndef __UDIALOGBEHAVIOR__
  25. #include "UDialogBehavior.h"
  26. #endif
  27.  
  28. #ifndef __UERRORMGR__
  29. #include "UErrorMgr.h"
  30. #endif
  31.  
  32. #ifndef __UGEOMETRY__
  33. #include "UGeometry.h"
  34. #endif
  35.  
  36. #ifndef __UITERATOR__
  37. #include "UIterator.h"
  38. #endif
  39.  
  40. #ifndef __UMACAPPGLOBALS__
  41. #include "UMacAppGlobals.h"
  42. #endif
  43.  
  44. #ifndef __UMACAPPUTILITIES__
  45. #include "UMacAppUtilities.h"
  46. #endif
  47.  
  48. #ifndef __UMEMORY__
  49. #include "UMemory.h"
  50. #endif
  51.  
  52. #ifndef __UPOPUP__
  53. #include "UPopup.h"
  54. #endif
  55.  
  56. #ifndef __USCROLLER__
  57. #include "UScroller.h"
  58. #endif
  59.  
  60. #ifndef __USTREAM__
  61. #include "UStream.h"
  62. #endif
  63.  
  64. #ifndef __UTECOMMANDS__
  65. #include "UTECommands.h"
  66. #endif
  67.  
  68. #ifndef __UVIEWSERVER__
  69. #include "UViewServer.h"
  70. #endif
  71.  
  72. #ifndef __UWINDOW__
  73. #include "UWindow.h"
  74. #endif
  75.  
  76. // Toolbox
  77.  
  78. #ifndef __FP__
  79. #include <fp.h>
  80. #endif
  81.  
  82. #ifndef __LOWMEM__
  83. #include <LowMem.h>
  84. #endif
  85.  
  86. #ifndef __PACKAGES__
  87. #include <Packages.h>
  88. #endif
  89.  
  90. #ifndef __RESOURCES__
  91. #include <Resources.h>
  92. #endif
  93.  
  94. #ifndef __STRINGS__
  95. #include <Strings.h>
  96. #endif
  97.  
  98. #ifndef __TOOLUTILS__
  99. #include <ToolUtils.h>
  100. #endif
  101.  
  102. // ANSI
  103.  
  104. #ifndef __STDIO__
  105. #include <stdio.h>
  106. #endif
  107.  
  108.  
  109. //========================================================================================
  110. // CLASS TButton
  111. //========================================================================================
  112. #undef Inherited
  113. #define Inherited TCtlMgr
  114.  
  115. #pragma segment DlgOpen
  116. MA_DEFINE_CLASS_M1(TButton,
  117.                    Inherited);
  118.  
  119. //----------------------------------------------------------------------------------------
  120. // TButton constructor
  121. //----------------------------------------------------------------------------------------
  122. #pragma segment DlgOpen
  123.  
  124. TButton::TButton()
  125. {
  126.     fEventNumber = mButtonHit;
  127.  
  128.     fStrListID = kNoResource;                    // buttons's label:    STR# rsrc id
  129.     fIndex = kNoResource;                        //                    index into STR#
  130. }
  131.  
  132. //----------------------------------------------------------------------------------------
  133. // TButton destructor
  134. //----------------------------------------------------------------------------------------
  135. #pragma segment MADestructorRes
  136.  
  137. TButton::~TButton()
  138. {
  139. }
  140.  
  141. //----------------------------------------------------------------------------------------
  142. // TButton::IButton: 
  143. //----------------------------------------------------------------------------------------
  144. #pragma segment DlgOpen
  145.  
  146. void TButton::IButton(TView* itsSuperView,
  147.                       const VPoint& itsLocation,
  148.                       const VPoint& itsSize,
  149.                       SizeDeterminer itsHSizeDet,
  150.                       SizeDeterminer itsVSizeDet,
  151.                       const CStr255& itsLabel)
  152. {
  153.     ICtlMgr(itsSuperView, itsLocation, itsSize, itsHSizeDet, itsVSizeDet, itsLabel, 0, 0, 0, ((pushButProc) | kControlUsesOwningWindowsFontVariant));
  154. }
  155.  
  156. //----------------------------------------------------------------------------------------
  157. // TButton::GetStandardSignature: 
  158. //----------------------------------------------------------------------------------------
  159. #pragma segment DlgWriteResource
  160.  
  161. IDType TButton::GetStandardSignature()            // Override 
  162. {
  163.     return kStdButton;
  164. }
  165.  
  166. //----------------------------------------------------------------------------------------
  167. // TButton::ReadFields: 
  168. //----------------------------------------------------------------------------------------
  169. #pragma segment DlgReadResource
  170.  
  171. void TButton::ReadFields(TStream* aStream)        // Override 
  172. {
  173.     Inherited::ReadFields(aStream);
  174.  
  175.     FailInfo fi;
  176.     Try(fi)
  177.     {
  178.         CStr255 itsLabel;
  179.         fStrListID = aStream->ReadInteger();
  180.         fIndex = aStream->ReadInteger();
  181.         if (fStrListID != kNoResource)            // retrieve the buttons's label from the resource
  182.             GetIndString(itsLabel, fStrListID, fIndex);
  183.         CreateCMgrControl(itsLabel, 0, 0, 0, ((pushButProc) | kControlUsesOwningWindowsFontVariant));
  184.         fi.Success();
  185.     }
  186.     else
  187.     {
  188.         Free();
  189.         fi.ReSignal();
  190.     }
  191. }
  192.  
  193. //----------------------------------------------------------------------------------------
  194. // TButton::WriteFields: 
  195. //----------------------------------------------------------------------------------------
  196. #pragma segment DlgWriteResource
  197.  
  198. void TButton::WriteFields(TStream* aStream)        // Override 
  199. {
  200.     Inherited::WriteFields(aStream);
  201.  
  202.     aStream->WriteInteger(fStrListID);
  203.     aStream->WriteInteger(fIndex);
  204. }
  205.  
  206. //----------------------------------------------------------------------------------------
  207. // TButton::DoEvent: 
  208. //----------------------------------------------------------------------------------------
  209. #pragma segment DlgRes
  210.  
  211. void TButton::DoEvent(EventNumber eventNumber,
  212.                       TEventHandler* source,
  213.                       TEvent* event)            // Override 
  214.  
  215. {
  216.     if ((eventNumber == GetEventNumber()) && (source != this) && IsEnabled())
  217.     {
  218.         if (!IsDimmed())                        // if dimmed, do absolutely nothing.
  219.         {
  220.             Flash();
  221.             Inherited::DoEvent(eventNumber, this, event);
  222.         }
  223.     }
  224.     else
  225.         Inherited::DoEvent(eventNumber, source, event);
  226. }
  227.  
  228.  
  229. //========================================================================================
  230. // CLASS TCheckBox
  231. //========================================================================================
  232. #undef Inherited
  233. #define Inherited TCtlMgr
  234.  
  235. #pragma segment DlgOpen
  236. MA_DEFINE_CLASS_M1(TCheckBox,
  237.                    Inherited);
  238.  
  239. //----------------------------------------------------------------------------------------
  240. // TCheckBox constructor
  241. //----------------------------------------------------------------------------------------
  242. #pragma segment DlgOpen
  243.  
  244. TCheckBox::TCheckBox()
  245. {
  246.     fEventNumber = mCheckBoxHit;
  247.  
  248.     fStrListID = kNoResource;                    // buttons's label:    STR# rsrc id
  249.     fIndex = kNoResource;                        //                    index into STR#
  250. }
  251.  
  252. //----------------------------------------------------------------------------------------
  253. // TCheckBox destructor
  254. //----------------------------------------------------------------------------------------
  255. #pragma segment MADestructorRes
  256.  
  257. TCheckBox::~TCheckBox()
  258. {
  259. }
  260.  
  261. //----------------------------------------------------------------------------------------
  262. // TCheckBox::ICheckBox: 
  263. //----------------------------------------------------------------------------------------
  264. #pragma segment DlgOpen
  265.  
  266. void TCheckBox::ICheckBox(TView* itsSuperView,
  267.                           const VPoint& itsLocation,
  268.                           const VPoint& itsSize,
  269.                           SizeDeterminer itsHSizeDet,
  270.                           SizeDeterminer itsVSizeDet,
  271.                           const CStr255& itsLabel,
  272.                           Boolean isTurnedOn)
  273.  
  274. {
  275.     ICtlMgr(itsSuperView, itsLocation, itsSize, itsHSizeDet, itsVSizeDet, itsLabel, 0, 0, 1, ((checkBoxProc) | kControlUsesOwningWindowsFontVariant));
  276.     SetState(isTurnedOn, kDontRedraw);
  277. }
  278.  
  279. //----------------------------------------------------------------------------------------
  280. // TCheckBox::GetStandardSignature: 
  281. //----------------------------------------------------------------------------------------
  282. #pragma segment DlgWriteResource
  283.  
  284. IDType TCheckBox::GetStandardSignature()        // Override 
  285. {
  286.     return kStdCheckBox;
  287. }
  288.  
  289. //----------------------------------------------------------------------------------------
  290. // TCheckBox::ReadFields: 
  291. //----------------------------------------------------------------------------------------
  292. #pragma segment DlgReadResource
  293.  
  294. void TCheckBox::ReadFields(TStream* aStream)    // Override 
  295. {
  296.     Inherited::ReadFields(aStream);
  297.  
  298.     FailInfo fi;
  299.     Try(fi)
  300.     {
  301.         Boolean isOn = aStream->ReadBoolean();
  302.  
  303.         CStr255 itsLabel;
  304.         fStrListID = aStream->ReadInteger();
  305.         fIndex = aStream->ReadInteger();
  306.         if (fStrListID != kNoResource)            // retrieve the buttons's label from the resource
  307.             GetIndString(itsLabel, fStrListID, fIndex);
  308.  
  309.         CreateCMgrControl(itsLabel, isOn, 0, 1, checkBoxProc | kControlUsesOwningWindowsFontVariant);
  310.         fi.Success();
  311.     }
  312.     else
  313.     {
  314.         Free();
  315.         fi.ReSignal();
  316.     }
  317. }
  318.  
  319. //----------------------------------------------------------------------------------------
  320. // TCheckBox::WriteFields: 
  321. //----------------------------------------------------------------------------------------
  322. #pragma segment DlgWriteResource
  323.  
  324. void TCheckBox::WriteFields(TStream* aStream)    // Override 
  325. {
  326.     Inherited::WriteFields(aStream);
  327.  
  328.     aStream->WriteBoolean(IsOn());
  329.  
  330.     aStream->WriteInteger(fStrListID);
  331.     aStream->WriteInteger(fIndex);
  332. }
  333.  
  334. //----------------------------------------------------------------------------------------
  335. // TCheckBox::DoEvent: 
  336. //----------------------------------------------------------------------------------------
  337. #pragma segment DlgRes
  338.  
  339. void TCheckBox::DoEvent(EventNumber eventNumber,
  340.                         TEventHandler* source,
  341.                         TEvent* event)            // Override 
  342.  
  343. {
  344.     if (eventNumber == mCheckBoxHit)
  345.         Toggle(kRedraw);
  346.     Inherited::DoEvent(eventNumber, source, event);
  347. }
  348.  
  349. //----------------------------------------------------------------------------------------
  350. // TCheckBox::IsOn: 
  351. //----------------------------------------------------------------------------------------
  352. #pragma segment DlgRes
  353.  
  354. Boolean TCheckBox::IsOn()
  355. {
  356.     return (GetLongVal() != 0);
  357. }
  358.  
  359. //----------------------------------------------------------------------------------------
  360. // TCheckBox::SetState: 
  361. //----------------------------------------------------------------------------------------
  362. #pragma segment DlgRes
  363.  
  364. void TCheckBox::SetState(Boolean state,
  365.                          Boolean redraw)
  366. {
  367.     SetLongVal((long)state, redraw);
  368. }
  369.  
  370. //----------------------------------------------------------------------------------------
  371. // TCheckBox::Toggle: 
  372. //----------------------------------------------------------------------------------------
  373. #pragma segment DlgRes
  374.  
  375. void TCheckBox::Toggle(Boolean redraw)
  376.  
  377. {
  378.     SetLongVal((long)(!IsOn()), redraw);
  379. }
  380.  
  381. //----------------------------------------------------------------------------------------
  382. // TCheckBox::ToggleIf: 
  383. //----------------------------------------------------------------------------------------
  384. #pragma segment DlgRes
  385.  
  386. void TCheckBox::ToggleIf(Boolean matchState,
  387.                          Boolean redraw)
  388. {
  389.     if (IsOn() == matchState)
  390.         SetLongVal((long)(!IsOn()), redraw);
  391. }
  392.  
  393.  
  394. //========================================================================================
  395. // CLASS TRadio
  396. //========================================================================================
  397. #undef Inherited
  398. #define Inherited TCtlMgr
  399.  
  400. #pragma segment DlgOpen
  401. MA_DEFINE_CLASS_M1(TRadio,
  402.                    Inherited);
  403.  
  404. //----------------------------------------------------------------------------------------
  405. // TRadio constructor
  406. //----------------------------------------------------------------------------------------
  407. #pragma segment DlgOpen
  408.  
  409. TRadio::TRadio()
  410. {
  411.     fEventNumber = mRadioHit;
  412.  
  413.     fStrListID = kNoResource;                    // buttons's label:    STR# rsrc id
  414.     fIndex = kNoResource;                        //                    index into STR#
  415. }
  416.  
  417. //----------------------------------------------------------------------------------------
  418. // TRadio destructor
  419. //----------------------------------------------------------------------------------------
  420. #pragma segment MADestructorRes
  421.  
  422. TRadio::~TRadio()
  423. {
  424. }
  425.  
  426. //----------------------------------------------------------------------------------------
  427. // TRadio::IRadio: 
  428. //----------------------------------------------------------------------------------------
  429. #pragma segment DlgOpen
  430.  
  431. void TRadio::IRadio(TView* itsSuperView,
  432.                     const VPoint& itsLocation,
  433.                     const VPoint& itsSize,
  434.                     SizeDeterminer itsHSizeDet,
  435.                     SizeDeterminer itsVSizeDet,
  436.                     const CStr255& itsLabel,
  437.                     Boolean isTurnedOn)
  438.  
  439. {
  440.     ICtlMgr(itsSuperView, itsLocation, itsSize, itsHSizeDet, itsVSizeDet, itsLabel, 0, 0, 1, ((radioButProc) | kControlUsesOwningWindowsFontVariant));
  441.  
  442.     FailInfo fi;
  443.     Try(fi)
  444.     {
  445.         SetState(isTurnedOn, kDontRedraw);
  446.  
  447.         if (IsOn())
  448.             HandleEvent(fEventNumber, this, NULL);
  449.  
  450.         fi.Success();
  451.     }
  452.     else
  453.     {
  454.         Free();
  455.         fi.ReSignal();
  456.     }
  457. }
  458.  
  459. //----------------------------------------------------------------------------------------
  460. // TRadio::GetStandardSignature: 
  461. //----------------------------------------------------------------------------------------
  462. #pragma segment DlgWriteResource
  463.  
  464. IDType TRadio::GetStandardSignature()            // Override 
  465. {
  466.     return kStdRadio;
  467. }
  468.  
  469. //----------------------------------------------------------------------------------------
  470. // TRadio::ReadFields: 
  471. //----------------------------------------------------------------------------------------
  472. #pragma segment DlgReadResource
  473.  
  474. void TRadio::ReadFields(TStream* aStream)        // Override 
  475. {
  476.     Inherited::ReadFields(aStream);
  477.  
  478.     FailInfo fi;
  479.     Try(fi)
  480.     {
  481.         Boolean isOn = aStream->ReadBoolean();
  482.  
  483.         CStr255 itsLabel;
  484.         fStrListID = aStream->ReadInteger();
  485.         fIndex = aStream->ReadInteger();
  486.         if (fStrListID != kNoResource)            // retrieve the buttons's label from the resource
  487.             GetIndString(itsLabel, fStrListID, fIndex);
  488.  
  489.         CreateCMgrControl(itsLabel, isOn, 0, 1, radioButProc | kControlUsesOwningWindowsFontVariant);
  490.         fi.Success();
  491.     }
  492.     else
  493.     {
  494.         Free();
  495.         fi.ReSignal();
  496.     }
  497. }
  498.  
  499. //----------------------------------------------------------------------------------------
  500. // TRadio::WriteFields: 
  501. //----------------------------------------------------------------------------------------
  502. #pragma segment DlgWriteResource
  503.  
  504. void TRadio::WriteFields(TStream* aStream)        // Override 
  505. {
  506.     Inherited::WriteFields(aStream);
  507.  
  508.     aStream->WriteBoolean(IsOn());
  509.  
  510.     aStream->WriteInteger(fStrListID);
  511.     aStream->WriteInteger(fIndex);
  512. }
  513.  
  514. //----------------------------------------------------------------------------------------
  515. // TRadio::DoPostCreate: 
  516. //----------------------------------------------------------------------------------------
  517. #pragma segment DlgOpen
  518.  
  519. void TRadio::DoPostCreate(TDocument* itsDocument)// Override
  520. {
  521.     Inherited::DoPostCreate(itsDocument);
  522.  
  523.     if (IsOn())
  524.         HandleEvent(fEventNumber, this, NULL);
  525. }
  526.  
  527. //----------------------------------------------------------------------------------------
  528. // TRadio::DoEvent: 
  529. //----------------------------------------------------------------------------------------
  530. #pragma segment DlgRes
  531.  
  532. void TRadio::DoEvent(EventNumber eventNumber,
  533.                      TEventHandler* source,
  534.                      TEvent* event)                // Override 
  535.  
  536. {
  537.     switch (eventNumber)
  538.     {
  539.         case mTurnOn:
  540.         case mTurnOff:
  541.             SetState(eventNumber == mTurnOn, kRedraw);
  542.             break;
  543.  
  544.         case mRadioHit:
  545.             if (!IsOn())
  546.                 Toggle(kRedraw);
  547.             // no break - drop through
  548.  
  549.         default:
  550.             Inherited::DoEvent(eventNumber, source, event);
  551.             break;
  552.     }
  553. }
  554.  
  555. //----------------------------------------------------------------------------------------
  556. // TRadio::IsOn: 
  557. //----------------------------------------------------------------------------------------
  558. #pragma segment DlgRes
  559.  
  560. Boolean TRadio::IsOn()
  561. {
  562.     return GetLongVal() != 0;
  563. }
  564.  
  565. //----------------------------------------------------------------------------------------
  566. // TRadio::SetState: 
  567. //----------------------------------------------------------------------------------------
  568. #pragma segment DlgRes
  569.  
  570. void TRadio::SetState(Boolean state,
  571.                       Boolean redraw)
  572. {
  573.     SetLongVal((long)(state), redraw);
  574. }
  575.  
  576. //----------------------------------------------------------------------------------------
  577. // TRadio::Toggle: 
  578. //----------------------------------------------------------------------------------------
  579. #pragma segment DlgRes
  580.  
  581. void TRadio::Toggle(Boolean redraw)
  582. {
  583.     SetLongVal((long)(!IsOn()), redraw);
  584. }
  585.  
  586. //----------------------------------------------------------------------------------------
  587. // TRadio::ToggleIf: 
  588. //----------------------------------------------------------------------------------------
  589. #pragma segment DlgRes
  590.  
  591. void TRadio::ToggleIf(Boolean matchState,
  592.                       Boolean redraw)
  593. {
  594.     Boolean isOn = IsOn();
  595.     if (isOn == matchState)
  596.         SetLongVal((long)(!isOn), redraw);
  597. }
  598.  
  599.  
  600. //========================================================================================
  601. // CLASS TIcon
  602. //========================================================================================
  603. #undef Inherited
  604. #define Inherited TControl
  605.  
  606. #pragma segment DlgOpen
  607. MA_DEFINE_CLASS_M1(TIcon,
  608.                    Inherited);
  609.  
  610. //----------------------------------------------------------------------------------------
  611. // TIcon constructor
  612. //----------------------------------------------------------------------------------------
  613. #pragma segment DlgOpen
  614.  
  615. TIcon::TIcon()
  616. {
  617.     fDataHandle = NULL;
  618.     fIsColor = kPreferColor;
  619.     fPreferColor = kPreferColor;
  620.     fRsrcID = kNoResource;
  621.  
  622.     fEventNumber = mIconHit;
  623. }
  624.  
  625. //----------------------------------------------------------------------------------------
  626. // TIcon::IIcon: 
  627. //----------------------------------------------------------------------------------------
  628. #pragma segment DlgOpen
  629.  
  630. void TIcon::IIcon(TView* itsSuperView,
  631.                   const VPoint& itsLocation,
  632.                   const VPoint& itsSize,
  633.                   SizeDeterminer itsHSizeDet,
  634.                   SizeDeterminer itsVSizeDet,
  635.                   ResNumber itsRsrcID,
  636.                   Boolean preferColor)
  637.  
  638. {
  639.     IControl(itsSuperView, itsLocation, itsSize, itsHSizeDet, itsVSizeDet);
  640.     fPreferColor = preferColor;
  641.     fIsColor = preferColor;
  642.  
  643.     FailInfo fi;
  644.     Try(fi)
  645.     {
  646.         SetIconRsrcID(itsRsrcID, kDontRedraw);
  647.  
  648.         fi.Success();
  649.     }
  650.     else                                        // Recover
  651.     {
  652.         Free();
  653.  
  654.         fi.ReSignal();
  655.     }
  656.     SetEnable(FALSE);                            // Default is to not enable hit testing
  657. }
  658.  
  659. //----------------------------------------------------------------------------------------
  660. // TIcon::Clone: 
  661. //----------------------------------------------------------------------------------------
  662. #pragma segment DlgNonRes
  663.  
  664. TObject* TIcon::Clone()                            // Override 
  665. {
  666.     MAVolatileInit(TIcon * , aClonedIcon, (TIcon *)(Inherited::Clone()));
  667.  
  668.     // don't call aClonedIcon->ReleaseIcon() b/c this will release my icon resource
  669.     // set initial values:
  670.     aClonedIcon->fDataHandle = NULL;
  671.     aClonedIcon->fIsColor = kPreferColor;
  672.     aClonedIcon->fPreferColor = kPreferColor;
  673.     aClonedIcon->fRsrcID = kNoResource;
  674.  
  675.     if (fDataHandle)
  676.     {
  677.         FailInfo fi;
  678.         Try(fi)
  679.         {
  680.             aClonedIcon->SetIconRsrcID(fRsrcID, kDontRedraw);
  681.             fi.Success();
  682.         }
  683.         else                                    // Recover
  684.         {
  685.             aClonedIcon->Free();
  686.             fi.ReSignal();
  687.         }
  688.     }
  689.  
  690.     return aClonedIcon;
  691. }
  692.  
  693. //----------------------------------------------------------------------------------------
  694. // TIcon::~TIcon: 
  695. //----------------------------------------------------------------------------------------
  696. #pragma segment DlgClose
  697.  
  698. TIcon::~TIcon()
  699. {
  700. }
  701.  
  702. //----------------------------------------------------------------------------------------
  703. // TIconSuite::Free
  704. //----------------------------------------------------------------------------------------
  705. #pragma segment DlgClose
  706.  
  707. void TIcon::Free()
  708. {
  709.     ReleaseIcon();
  710.  
  711.     Inherited::Free();
  712. }
  713.  
  714. //----------------------------------------------------------------------------------------
  715. // TIcon::GetStandardSignature: 
  716. //----------------------------------------------------------------------------------------
  717. #pragma segment DlgWriteResource
  718.  
  719. IDType TIcon::GetStandardSignature()            // Override 
  720. {
  721.     return kStdIcon;
  722. }
  723.  
  724. //----------------------------------------------------------------------------------------
  725. // TIcon::ReadFields: 
  726. //----------------------------------------------------------------------------------------
  727. #pragma segment DlgReadResource
  728.  
  729. void TIcon::ReadFields(TStream* aStream)        // Override 
  730. {
  731.     Inherited::ReadFields(aStream);
  732.  
  733.     FailInfo fi;
  734.     Try(fi)
  735.     {
  736.         fPreferColor = aStream->ReadBoolean();
  737.  
  738.         fIsColor = fPreferColor;
  739.  
  740.         ResNumber itsRsrcID = aStream->ReadInteger();
  741.         SetIconRsrcID(itsRsrcID, kDontRedraw);
  742.         fi.Success();
  743.     }
  744.     else                                        // Recover
  745.     {
  746.         Free();
  747.         fi.ReSignal();
  748.     }
  749. }
  750.  
  751. //----------------------------------------------------------------------------------------
  752. // TIcon::WriteFields: 
  753. //----------------------------------------------------------------------------------------
  754. #pragma segment DlgWriteResource
  755.  
  756. void TIcon::WriteFields(TStream* aStream)        // Override 
  757. {
  758.     Inherited::WriteFields(aStream);
  759.  
  760.     aStream->WriteBoolean(fPreferColor);
  761.  
  762. #if qDebugMsg
  763.     if (fRsrcID == kNoResource)
  764.         fprintf(stderr, "Writing a TIcon with no resource ID.\n");
  765. #endif
  766.  
  767.     aStream->WriteInteger(fRsrcID);
  768. }
  769.  
  770. //----------------------------------------------------------------------------------------
  771. // TIcon::Draw: 
  772. //----------------------------------------------------------------------------------------
  773. #pragma segment DlgRes
  774.  
  775. void TIcon::Draw(const VRect& area)                // Override 
  776. {
  777.     if (fDataHandle)
  778.     {
  779.         if (IsAResource(fDataHandle))
  780.             LoadResource(fDataHandle);
  781.         if (*fDataHandle)                        // If there's room for the icon… 
  782.         {
  783.             PenNormal();
  784.             CRect theQDRect(ControlQDArea());
  785.  
  786.             SignedByte oldState = HGetState(fDataHandle);
  787.             HNoPurge(fDataHandle);
  788.             HLock(fDataHandle);
  789.  
  790.             if (fIsColor)
  791.             {
  792.                 // We can't use PlotCIcon here because it can't be written to a picture 
  793.                 // and when WriteToDeskScrap is called, the icon is plotted on the 
  794.                 // desktop rather than in the picture.  So instead, pick apart the color 
  795.                 // icon handle and use copybits, ignoring the mask. 
  796.  
  797.                 PixMap aPixMap = (**((CIconHandle)fDataHandle)).iconPMap;
  798.                 SignedByte wasState = LockHandle((**((CIconHandle)fDataHandle)).iconData);
  799.                 aPixMap.baseAddr = *((**((CIconHandle)fDataHandle)).iconData);
  800.                 CRect srcRect(aPixMap.bounds);
  801.                 BitMapPtr aBitMapPtr = (BitMapPtr) & aPixMap;
  802.                 CopyBits(aBitMapPtr, &(qd.thePort->portBits), srcRect, theQDRect, srcCopy, NULL);
  803.                 HSetState((**((CIconHandle)fDataHandle)).iconData, wasState);
  804.             }
  805.             else
  806.                 PlotIcon(theQDRect, fDataHandle);
  807.  
  808.             HSetState(fDataHandle, oldState);
  809.         }
  810.     }
  811.  
  812.     Inherited::Draw(area);
  813. }
  814.  
  815. //----------------------------------------------------------------------------------------
  816. // TIcon::ReleaseIcon: 
  817. //----------------------------------------------------------------------------------------
  818. #pragma segment DlgNonRes
  819.  
  820. void TIcon::ReleaseIcon()
  821. {
  822.     fRsrcID = kNoResource;
  823.     if (fDataHandle)
  824.     {
  825.         if (fIsColor)
  826.             DisposeCIcon((CIconHandle)fDataHandle);
  827.         else
  828.             HPurge(fDataHandle);
  829.         fDataHandle = NULL;
  830.     }
  831. }
  832.  
  833. //----------------------------------------------------------------------------------------
  834. // TIcon::SetIcon: 
  835. //----------------------------------------------------------------------------------------
  836. #pragma segment DlgNonRes
  837.  
  838. void TIcon::SetIcon(Handle theIcon,
  839.                     Boolean redraw)
  840. {
  841.     ReleaseIcon();
  842.  
  843.     // Make sure the resource is loaded since it may have been purged and we
  844.     // want to be sure that we can get the size to determine the correct type
  845.     // of icon.
  846.     LoadResource(theIcon);
  847.     FailResError();
  848.     fPreferColor = (GetHandleSize(theIcon) != 128);
  849.  
  850.     fDataHandle = theIcon;
  851.  
  852.     // get the rsrc id (this will only work for b&w icons)
  853.     ResNumber theID;
  854.     ResType theType;
  855.     CStr255 name;
  856.  
  857.     ::GetResInfo(theIcon, &theID, &theType, name);
  858.     if (ResError() == noErr)
  859.         fRsrcID = theID;
  860.  
  861.     if (redraw)
  862.         ForceRedraw();
  863. }
  864.  
  865. //----------------------------------------------------------------------------------------
  866. // TIcon::SetIconRsrcID: 
  867. //----------------------------------------------------------------------------------------
  868. #pragma segment DlgNonRes
  869.  
  870. void TIcon::SetIconRsrcID(ResNumber itsRsrcID,
  871.                           Boolean redraw)
  872. {
  873.     ReleaseIcon();
  874.  
  875.     fRsrcID = itsRsrcID;
  876.     if (fRsrcID != kNoResource)
  877.     {
  878.         // try to get a 'cicn'
  879.         if (fPreferColor && (qNeedsColorQD || HasColorQD()))
  880.         {
  881.             // make the 'cicn' resource non-purgeable, so the Toolbox doesn't die 
  882.             Handle itsRsrcHandle = ::GetResource('cicn', fRsrcID);
  883.             if (itsRsrcHandle)
  884.             {
  885.                 SignedByte savedState = ::HGetState(itsRsrcHandle);
  886.                 HNoPurge(itsRsrcHandle);
  887.  
  888.                 fDataHandle = (Handle)::GetCIcon(fRsrcID);
  889.  
  890.                 // restore the state of the 'cicn' resource 
  891.                 HSetState(itsRsrcHandle, savedState);
  892.             }
  893.         }
  894.  
  895.         if (fDataHandle)
  896.             fIsColor = kPreferColor;
  897.         else                                    // try to get an 'ICON'
  898.         {
  899.             fDataHandle = ::GetIcon(fRsrcID);
  900.             if (fDataHandle)
  901.                 fIsColor = !kPreferColor;    // Either can't or won't 
  902.         }
  903.  
  904.         FailNILResource(fDataHandle);
  905.     }
  906.  
  907.     if (redraw)
  908.         ForceRedraw();
  909. }
  910.  
  911.  
  912. //========================================================================================
  913. // CLASS TIconSuite
  914. //========================================================================================
  915. #undef Inherited
  916. #define Inherited TIcon
  917.  
  918. #pragma segment DlgOpen
  919. MA_DEFINE_CLASS_M1(TIconSuite,
  920.                    Inherited);
  921.  
  922. //----------------------------------------------------------------------------------------
  923. // TIconSuite::TIconSuite
  924. //----------------------------------------------------------------------------------------
  925. #pragma segment DlgOpen
  926.  
  927. TIconSuite::TIconSuite()
  928. {
  929.     fHasSuite = false;
  930. }
  931.  
  932. //----------------------------------------------------------------------------------------
  933. // TIconSuite::IIconSuiteView
  934. //----------------------------------------------------------------------------------------
  935. #pragma segment DlgOpen
  936.  
  937. void TIconSuite::IIconSuite(TView* itsSuperView,
  938.                             const VPoint& itsLocation,
  939.                             const VPoint& itsSize,
  940.                             SizeDeterminer itsHSizeDet,
  941.                             SizeDeterminer itsVSizeDet,
  942.                             ResNumber itsRsrcID,
  943.                             Boolean preferColor)
  944. {
  945.     IIcon(itsSuperView, itsLocation, itsSize, itsHSizeDet, itsVSizeDet, itsRsrcID, preferColor);
  946. }
  947.  
  948.  
  949. //----------------------------------------------------------------------------------------
  950. // TIconSuite::ReadFields: 
  951. //----------------------------------------------------------------------------------------
  952. #pragma segment DlgReadResource
  953.  
  954. void TIconSuite::ReadFields(TStream* aStream)    // Override 
  955. {
  956.     fAlignmentType = atNone;
  957.     fNormalTransform = ttNone;
  958.     fHiliteTransform = ttSelected;
  959.     fDimTransform = ttOffline;
  960.     fHasSuite = false;
  961.  
  962.     Inherited::ReadFields(aStream);
  963. }
  964.  
  965. //----------------------------------------------------------------------------------------
  966. // TIconSuite::~TIconSuite
  967. //----------------------------------------------------------------------------------------
  968. TIconSuite::~TIconSuite()
  969. {
  970. }
  971.  
  972. //----------------------------------------------------------------------------------------
  973. // TIconSuite::GetStandardSignature: 
  974. //----------------------------------------------------------------------------------------
  975. #pragma segment DlgWriteResource
  976.  
  977. IDType TIconSuite::GetStandardSignature()        // Override 
  978. {
  979.     return kStdIconSuite;
  980. }
  981.  
  982. //----------------------------------------------------------------------------------------
  983. // TIconSuite::Draw
  984. //----------------------------------------------------------------------------------------
  985. #pragma segment DlgRes
  986.  
  987. void TIconSuite::Draw(const VRect& area)
  988. {
  989.     if (fHasSuite)
  990.     {
  991.         IconTransformType itsTransform = fNormalTransform;
  992.         if (fHilite)
  993.             itsTransform += fHiliteTransform;
  994.  
  995.         if (fDimmed)
  996.             itsTransform += fDimTransform;
  997.  
  998.         ::PlotIconSuite(&GetIconRect(), fAlignmentType, itsTransform, fDataHandle);
  999.     }
  1000.     else
  1001.         Inherited::Draw(area);
  1002. }
  1003.  
  1004. //----------------------------------------------------------------------------------------
  1005. // TIconSuite::GetExtentRegion
  1006. //----------------------------------------------------------------------------------------
  1007. #pragma segment MAViewRes
  1008.  
  1009. void TIconSuite::GetExtentRegion(RgnHandle itsExtentRgn)
  1010. {
  1011.     if (fHasSuite)
  1012.         IconSuiteToRgn(itsExtentRgn, &GetIconRect(), fAlignmentType, fDataHandle);
  1013.     else
  1014.         Inherited::GetExtentRegion(itsExtentRgn);
  1015. }
  1016.  
  1017. //----------------------------------------------------------------------------------------
  1018. // TIconSuite::GetExtentRegion
  1019. //----------------------------------------------------------------------------------------
  1020. #pragma segment MAViewRes
  1021.  
  1022. CRect TIconSuite::GetIconRect()
  1023. {
  1024.     //
  1025.     // Return a rect that will cause the similarly sized icon to be retrieved
  1026.     // and displayed without any stretching. It will be clipped, however.
  1027.     //
  1028.     CPoint botRightPoint;
  1029.  
  1030.     if (fSize.h <= 16 && fSize.v <= 12)
  1031.         botRightPoint = CPoint(16, 12);
  1032.     else if (fSize.h <= 16 && fSize.v <= 16)
  1033.         botRightPoint = CPoint(16, 16);
  1034.     else
  1035.         botRightPoint = CPoint(32, 32);
  1036.  
  1037.     return CRect(gZeroPt, botRightPoint);
  1038. }
  1039.  
  1040. //----------------------------------------------------------------------------------------
  1041. // TIconSuite::Hilite
  1042. //----------------------------------------------------------------------------------------
  1043. #pragma segment DlgRes
  1044.  
  1045. void TIconSuite::Hilite()
  1046. {
  1047.     if (fHasSuite)
  1048.     {
  1049.         // do nothing
  1050.     }
  1051.     else
  1052.         Inherited::Hilite();
  1053. }
  1054.  
  1055. //----------------------------------------------------------------------------------------
  1056. // TIconSuite::HiliteState
  1057. //----------------------------------------------------------------------------------------
  1058. #pragma segment DlgNonRes
  1059.  
  1060. void TIconSuite::HiliteState(Boolean state,
  1061.                              Boolean redraw)
  1062. {
  1063.     if (fHasSuite)
  1064.     {
  1065.         if (state != fHilite)
  1066.         {
  1067.             fHilite = state;
  1068.             if (redraw)
  1069.                 DrawContents();
  1070.         }
  1071.     }
  1072.     else
  1073.         Inherited::HiliteState(state, redraw);
  1074. }
  1075.  
  1076. //----------------------------------------------------------------------------------------
  1077. // TIconSuite::Dim
  1078. //----------------------------------------------------------------------------------------
  1079. #pragma segment DlgRes
  1080.  
  1081. void TIconSuite::Dim()
  1082. {
  1083.     if (fHasSuite)
  1084.     {
  1085.         // do nothing
  1086.     }
  1087.     else
  1088.         Inherited::Dim();
  1089. }
  1090.  
  1091. //----------------------------------------------------------------------------------------
  1092. // TIconSuite::HiliteState
  1093. //----------------------------------------------------------------------------------------
  1094. #pragma segment DlgNonRes
  1095.  
  1096. void TIconSuite::DimState(Boolean state,
  1097.                           Boolean redraw)
  1098. {
  1099.     if (fHasSuite)
  1100.     {
  1101.         if (state != fDimmed)
  1102.         {
  1103.             fDimmed = state;
  1104.             if (redraw)
  1105.                 DrawContents();
  1106.         }
  1107.     }
  1108.     else
  1109.         Inherited::DimState(state, redraw);
  1110. }
  1111.  
  1112. //----------------------------------------------------------------------------------------
  1113. // TIconSuite::ReleaseIcon
  1114. //----------------------------------------------------------------------------------------
  1115. #pragma segment DlgNonRes
  1116.  
  1117. void TIconSuite::ReleaseIcon()
  1118. {
  1119.     if (fHasSuite)
  1120.     {
  1121.         if (fDataHandle)
  1122.             FailOSErr(::DisposeIconSuite(fDataHandle, false));
  1123.         fDataHandle = NULL;
  1124.         fRsrcID = kNoResource;
  1125.         fHasSuite = FALSE;
  1126.     }
  1127.     else
  1128.         Inherited::ReleaseIcon();
  1129. }
  1130.  
  1131. //----------------------------------------------------------------------------------------
  1132. // TIconSuite::SetIconRsrcID
  1133. //----------------------------------------------------------------------------------------
  1134. #pragma segment DlgNonRes
  1135.  
  1136. void TIconSuite::SetIconRsrcID(ResNumber itsRsrcID,
  1137.                                Boolean redraw)
  1138. {
  1139.     ReleaseIcon();
  1140.  
  1141.     if (itsRsrcID != kNoResource)
  1142.     {
  1143.         Handle theSuite = NULL;
  1144.         OSErr theErr = ::GetIconSuite(&theSuite, itsRsrcID, svAllAvailableData);
  1145.  
  1146.         if (theErr == noErr && theSuite)
  1147.         {
  1148.             fRsrcID = itsRsrcID;
  1149.             fDataHandle = theSuite;
  1150.             fHasSuite = TRUE;
  1151.             fIsColor = kPreferColor;
  1152.  
  1153.             if (redraw)
  1154.                 ForceRedraw();
  1155.         }
  1156.         else
  1157.             Inherited::SetIconRsrcID(itsRsrcID, redraw);
  1158.     }
  1159. }
  1160.  
  1161.  
  1162. //========================================================================================
  1163. // CLASS TSmallIcon
  1164. //========================================================================================
  1165. #undef Inherited
  1166. #define Inherited TControl
  1167.  
  1168. #pragma segment DlgOpen
  1169. MA_DEFINE_CLASS_M1(TSmallIcon,
  1170.                    Inherited);
  1171.  
  1172. //----------------------------------------------------------------------------------------
  1173. // TSmallIcon constructor
  1174. //----------------------------------------------------------------------------------------
  1175. #pragma segment DlgOpen
  1176.  
  1177. TSmallIcon::TSmallIcon()
  1178. {
  1179.     fDataHandle = NULL;
  1180.     fRsrcID = kNoResource;
  1181.     fEventNumber = mSmallIconHit;
  1182. }
  1183.  
  1184. //----------------------------------------------------------------------------------------
  1185. // TSmallIcon destructor
  1186. //----------------------------------------------------------------------------------------
  1187. #pragma segment MADestructorRes
  1188.  
  1189. TSmallIcon::~TSmallIcon()
  1190. {
  1191. }
  1192.  
  1193. //----------------------------------------------------------------------------------------
  1194. // TSmallIcon::ISmallIcon: 
  1195. //----------------------------------------------------------------------------------------
  1196. #pragma segment DlgOpen
  1197.  
  1198. void TSmallIcon::ISmallIcon(TView* itsSuperView,
  1199.                             const VPoint& itsLocation,
  1200.                             const VPoint& itsSize,
  1201.                             SizeDeterminer itsHSizeDet,
  1202.                             SizeDeterminer itsVSizeDet,
  1203.                             ResNumber itsRsrcID)
  1204. {
  1205.     IControl(itsSuperView, itsLocation, itsSize, itsHSizeDet, itsVSizeDet);
  1206.  
  1207.     FailInfo fi;
  1208.     Try(fi)
  1209.     {
  1210.         SetSmallIcon(itsRsrcID, kDontRedraw);
  1211.  
  1212.         fi.Success();
  1213.     }
  1214.     else                                        // Recover
  1215.     {
  1216.         Free();
  1217.  
  1218.         fi.ReSignal();
  1219.     }
  1220.  
  1221.     SetEnable(FALSE);                            // Default is to not enable hit testing 
  1222. }
  1223.  
  1224. //----------------------------------------------------------------------------------------
  1225. // TSmallIcon::Clone: 
  1226. //----------------------------------------------------------------------------------------
  1227. #pragma segment DlgNonRes
  1228.  
  1229. TObject* TSmallIcon::Clone()                    // Override 
  1230. {
  1231.     MAVolatileInit(TSmallIcon * , aClonedSmallIcon, (TSmallIcon *)Inherited::Clone());
  1232.  
  1233.     // don't call aClonedSmallIcon->ReleaseSmallIcon() b/c this will release my icon resource
  1234.     // set initial values:
  1235.     aClonedSmallIcon->fRsrcID = kNoResource;
  1236.     aClonedSmallIcon->fDataHandle = NULL;
  1237.  
  1238.     if (fDataHandle)
  1239.     {
  1240.         FailInfo fi;
  1241.         Try(fi)
  1242.         {
  1243.             aClonedSmallIcon->SetSmallIcon(fRsrcID, kDontRedraw);
  1244.             fi.Success();
  1245.         }
  1246.         else                                    // Recover
  1247.         {
  1248.             aClonedSmallIcon->Free();
  1249.             fi.ReSignal();
  1250.         }
  1251.     }
  1252.  
  1253.     return aClonedSmallIcon;
  1254. }
  1255.  
  1256.  
  1257. //----------------------------------------------------------------------------------------
  1258. // TSmallIcon::GetStandardSignature: 
  1259. //----------------------------------------------------------------------------------------
  1260. #pragma segment DlgWriteResource
  1261.  
  1262. IDType TSmallIcon::GetStandardSignature()        // Override 
  1263. {
  1264.     return kStdSmallIcon;
  1265. }
  1266.  
  1267. //----------------------------------------------------------------------------------------
  1268. // TSmallIcon::ReadFields: 
  1269. //----------------------------------------------------------------------------------------
  1270. #pragma segment DlgReadResource
  1271.  
  1272. void TSmallIcon::ReadFields(TStream* aStream)    // Override 
  1273. {
  1274.     Inherited::ReadFields(aStream);
  1275.  
  1276.     FailInfo fi;
  1277.     Try(fi)
  1278.     {
  1279.         ResNumber itsRsrcID = aStream->ReadInteger();
  1280.         SetSmallIcon(itsRsrcID, kDontRedraw);
  1281.         fi.Success();
  1282.     }
  1283.     else                                        // Recover
  1284.     {
  1285.         Free();
  1286.         fi.ReSignal();
  1287.     }
  1288. }
  1289.  
  1290. //----------------------------------------------------------------------------------------
  1291. // TSmallIcon::WriteFields: 
  1292. //----------------------------------------------------------------------------------------
  1293. #pragma segment DlgWriteResource
  1294.  
  1295. void TSmallIcon::WriteFields(TStream* aStream)    // Override 
  1296. {
  1297.     Inherited::WriteFields(aStream);
  1298.  
  1299. #if qDebugMsg
  1300.     if (fRsrcID == kNoResource)
  1301.         fprintf(stderr, "Wrote a TSmallIcon with no resource ID.\n");
  1302. #endif
  1303.  
  1304.     aStream->WriteInteger(fRsrcID);
  1305. }
  1306.  
  1307. //----------------------------------------------------------------------------------------
  1308. // TSmallIcon::Draw: 
  1309. //----------------------------------------------------------------------------------------
  1310. #pragma segment DlgRes
  1311.  
  1312. void TSmallIcon::Draw(const VRect& area)        // Override 
  1313. {
  1314. #if qDebug
  1315.     AssumeFocused();
  1316. #endif
  1317.  
  1318.     if (fDataHandle)
  1319.     {
  1320.         if (IsAResource(fDataHandle))
  1321.             LoadResource(fDataHandle);
  1322.         if (*fDataHandle)                        // If there's room for the small icon… 
  1323.         {
  1324.             SignedByte oldState = HGetState(fDataHandle);
  1325.             HNoPurge(fDataHandle);
  1326.  
  1327.             // construct a source BitMap 
  1328.             BitMap srcBits;
  1329.             srcBits.baseAddr = (*fDataHandle);
  1330.             srcBits.rowBytes = 2;
  1331.             SetRect(&(srcBits.bounds), 0, 0, 16, 16);
  1332.  
  1333.             GrafPtr port;
  1334.             GetPort(&port);
  1335.             CopyBits(&srcBits, &(port->portBits), &(srcBits.bounds), &ControlQDArea(), srcCopy, NULL);
  1336.             HSetState(fDataHandle, oldState);
  1337.         }
  1338.     }
  1339.  
  1340.     Inherited::Draw(area);
  1341. }
  1342.  
  1343. //----------------------------------------------------------------------------------------
  1344. // TSmallIcon::ReleaseSmallIcon: 
  1345. //----------------------------------------------------------------------------------------
  1346. #pragma segment DlgNonRes
  1347.  
  1348. void TSmallIcon::ReleaseSmallIcon()
  1349. {
  1350.     fRsrcID = kNoResource;
  1351.  
  1352.     if (fDataHandle)
  1353.     {
  1354.         HPurge(fDataHandle);
  1355.         fDataHandle = NULL;
  1356.     }
  1357. }
  1358.  
  1359. //----------------------------------------------------------------------------------------
  1360. // TSmallIcon::SetSmallIcon: 
  1361. //----------------------------------------------------------------------------------------
  1362. #pragma segment DlgNonRes
  1363.  
  1364. void TSmallIcon::SetSmallIcon(ResNumber theSmallIcon,
  1365.                               Boolean redraw)
  1366. {
  1367.     ReleaseSmallIcon();
  1368.  
  1369.     fRsrcID = theSmallIcon;
  1370.  
  1371.     if (fRsrcID != kNoResource)
  1372.     {
  1373.         fDataHandle = GetResource('SICN', fRsrcID);
  1374.         FailNILResource(fDataHandle);            // was FailResError();
  1375.     }
  1376.  
  1377.     if (redraw)
  1378.         ForceRedraw();
  1379. }
  1380.  
  1381.  
  1382. //----------------------------------------------------------------------------------------
  1383. // End of UDialog.More.cp
  1384.  
  1385. #pragma segment Inline
  1386.  
  1387.